//
//  QBAnimationView.swift
//  QBLaunchAnimation
//
//  Created by 乐启榜 on 16/8/26.
//  Copyright © 2016年 乐启榜. All rights reserved.
//

import UIKit

protocol QBAnimationViewDelegate: class {
    
    func completeAnimation()
    
}

class QBAnimationView: UIView {

    let circleLayer = CircleLayer()
    let triangleLayer = TriangleLayer()
    let redRectangleLayer = RectangleLayer()
    let blueRectangleLayer = RectangleLayer()
    let waveLayer = WaveLayer()
    
    var parentFrmae: CGRect = CGRectZero
    
    weak var delegate: QBAnimationViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.clearColor()
        addCircleLayer()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     添加动画，圆
     */
    func addCircleLayer() {
        layer.addSublayer(circleLayer)
        circleLayer.expand()
        NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: #selector(wobbleCircleLayer), userInfo: nil, repeats: false)
    }
    /**
     添加压扁动画
     */
    func wobbleCircleLayer() {
        circleLayer.wobbleAnimation()
        layer.addSublayer(triangleLayer)
        NSTimer.scheduledTimerWithTimeInterval(0.9, target: self, selector: #selector(showTriangleAnimation), userInfo: nil, repeats: false)
    }
    // 三角形伸出
    func showTriangleAnimation()  {
        triangleLayer.triangleAnimation()
        NSTimer.scheduledTimerWithTimeInterval(0.9, target: self, selector: #selector(transformAnimation), userInfo: nil, repeats: false)
        
    }
    // 旋转，圆变小
    func transformAnimation() {
        transformRotation()// 旋转
        circleLayer.contract()// 圆变小
        NSTimer.scheduledTimerWithTimeInterval(0.45, target: self, selector: #selector(drawRedRectangleAnimation), userInfo: nil, repeats: false)
        NSTimer.scheduledTimerWithTimeInterval(0.65, target: self, selector: #selector(drawBlueRectangleAnimation), userInfo: nil, repeats: false)
        
    }
    
    func transformRotation() {
        self.layer.anchorPoint = CGPointMake(0.5, 0.65)// 锚点
        let rotationAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnimation.toValue = CGFloat(M_PI * 2)
        rotationAnimation.duration = 0.45
        rotationAnimation.removedOnCompletion = false
        layer.addAnimation(rotationAnimation, forKey: nil)
        
    }
    
    func drawRedRectangleAnimation() {
        layer.addSublayer(redRectangleLayer)
        redRectangleLayer.strokeChangeWithColor(UIColor.colorWithHexString("#da70d6"))
        
    }
    func drawBlueRectangleAnimation() {
        layer.addSublayer(blueRectangleLayer)
        blueRectangleLayer.strokeChangeWithColor(UIColor.colorWithHexString("#40e0b0"))
        NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: #selector(drawWaveAnimation), userInfo: nil, repeats: false)
    }
    
    // 波浪填充
    func drawWaveAnimation() {
        layer.addSublayer(waveLayer)
        waveLayer.animation()
        NSTimer.scheduledTimerWithTimeInterval(0.9, target: self, selector: #selector(expandView), userInfo: nil, repeats: false)
    }
    
    func expandView() {
        backgroundColor = UIColor.colorWithHexString("#40e0b0")
        frame = CGRect(x: frame.origin.x - blueRectangleLayer.lineWidth,
                       y: frame.origin.y - blueRectangleLayer.lineWidth,
                       width: frame.size.width + blueRectangleLayer.lineWidth * 2,
                       height: frame.size.height + blueRectangleLayer.lineWidth * 2)
        layer.sublayers = nil
        
        weak var weakSelf = self
        guard let _weakSelf = weakSelf else{return}
        UIView.animateWithDuration(0.3, animations: { 
            _weakSelf.frame = _weakSelf.parentFrmae
            }) { (finished) in
            _weakSelf.delegate?.completeAnimation()
        }
        
    }

}
